package com.fjec.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjec.base.entity.SysAgreement;
import com.fjec.base.mapper.SysAgreementMapper;
import com.fjec.base.service.ISysAgreementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjec.base.view.AgreementVO;
import com.fjec.common.enums.EnumSysCode;
import com.fjec.common.protocol.JsonResult;
import com.fjec.common.util.DateUtil;
import com.fjec.common.util.SequenceIdGenerator;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 006.协议表 服务实现类
 * </p>
 *
 * @author admin
 * @since 2020-08-31
 */
@Service
@Transactional
public class SysAgreementServiceImpl extends ServiceImpl<SysAgreementMapper, SysAgreement> implements ISysAgreementService {

    @Resource
    private SysAgreementMapper mapper;

    @Resource
    private SequenceIdGenerator idGenerator;

    @Override
    public JsonResult selectPageByCode(Page page, String code) {
        QueryWrapper<SysAgreement> query = new QueryWrapper<>();
        query.eq("agreeCode", code);
        Page<SysAgreement> result = mapper.selectPage(page, query);
        List<SysAgreement> sysAgreementList = result.getRecords();
        List<AgreementVO> resultList = new ArrayList<>();
        for (SysAgreement entity : sysAgreementList) {
            AgreementVO view = new AgreementVO();
            try {
                BeanUtils.copyProperties(view, entity);
            } catch (Exception e) {
                return JsonResult.FAILURE;
            }
            resultList.add(view);
        }
        return JsonResult.success().data(resultList).build();
    }

    @Override
    public JsonResult selectById(Long id) {
        SysAgreement entity = mapper.selectById(id);
        if (entity == null) {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0008.getCode())
                    .message(EnumSysCode.ERR_0008.getMsg())
                    .build();
        }
        AgreementVO view = new AgreementVO();
        try {
            BeanUtils.copyProperties(view, entity);
        } catch (Exception e) {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0038.getCode())
                    .message(EnumSysCode.ERR_0038.getMsg())
                    .build();
        }
        return JsonResult.success().data(view).build();
    }

    @Override
    public JsonResult save(String code, String content, Integer ver, String title, Integer value) {
        SysAgreement entity = new SysAgreement();
        entity.setId(idGenerator.nextId());
        entity.setAgreeCode(code);
        entity.setAgreeContent(content);
        entity.setAgreeVer(ver);
        entity.setCreateTime(DateUtil.nowLdt());
        entity .setValid(0);
        entity.setAgreeTitle(title);
        entity.setAgreeValue(value);
        this.save(entity);
        return JsonResult.SUCCESS;
    }

    @Override
    public JsonResult update(Long id, String code, String content, Integer ver, String title, Integer value) {
        SysAgreement entity = mapper.selectById(id);
        if (entity == null) {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0008.getCode())
                    .message(EnumSysCode.ERR_0008.getMsg())
                    .build();
        }
        if (Strings.isNotEmpty(code)) {
            entity.setAgreeCode(code);
        }
        if (Strings.isNotEmpty(content)) {
            entity.setAgreeContent(content);
        }
        if (!Objects.isNull(ver)) {
            entity.setAgreeVer(ver);
        }
        if (Strings.isNotEmpty(title)) {
            entity.setAgreeTitle(title);
        }
        if (!Objects.isNull(value)) {
            entity.setAgreeValue(value);
        }
        this.updateById(entity);
        return JsonResult.SUCCESS;
    }

    @Override
    public JsonResult getAgreement(String argeeCode, Integer valid) {
        QueryWrapper<SysAgreement> query = new QueryWrapper<>();
        query.eq("agreeCode", argeeCode).eq("valid", valid);
        SysAgreement entity = this.getOne(query);
        AgreementVO view = new AgreementVO();
        try {
            BeanUtils.copyProperties(view, entity);
        } catch (Exception e) {
            return JsonResult.FAILURE;
        }
        return JsonResult.success().data(view).build();
    }

    @Override
    public JsonResult setAgreementValid(Long id) {
        SysAgreement entity = mapper.selectById(id);
        if (entity == null) {
            return JsonResult.failure()
                    .code(EnumSysCode.ERR_0008.getCode())
                    .message(EnumSysCode.ERR_0008.getMsg())
                    .build();
        }
        UpdateWrapper<SysAgreement> wrapper = new UpdateWrapper<>();
        wrapper.eq("agreeCode", entity.getAgreeCode());
        wrapper.set("valid", 0);
        this.update(wrapper);
        entity.setValid(1);
        this.updateById(entity);
        return JsonResult.SUCCESS;
    }
}
